36. 有效的数独
为保证权益,题目请参考 36. 有效的数独(From LeetCode).
解决方案1
Python
python
# 36. 有效的数独
# https://leetcode-cn.com/problems/valid-sudoku/
################################################################################
from typing import List
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
# row
for i in range(9):
t = set()
for j in range(9):
if board[i][j] != ".":
if board[i][j] in t:
return False
else:
t.add(board[i][j])
# column
for i in range(9):
t = set()
for j in range(9):
if board[j][i] != ".":
if board[j][i] in t:
return False
else:
t.add(board[j][i])
# 3x3
for i in range(0, 9, 3):
for j in range(0, 9, 3):
t = set()
for m in range(i, i + 3):
for n in range(j, j + 3):
if board[m][n] != ".":
if board[m][n] in t:
return False
else:
t.add(board[m][n])
return True
################################################################################
if __name__ == "__main__":
solution = Solution()
print(
solution.isValidSudoku(
[
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
]
)
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
解决方案2
CPP
C++
/*
* https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/30/
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution
{
public:
bool isValidSudoku(vector<vector<char>> &board)
{
//column
for (int i = 0; i < 9; ++i)
{
bool nums[10] = {false};
for (int j = 0; j < 9; ++j)
{
int tmp = board[i][j] - '0';
if (tmp >= 0 && tmp <= 9)
{
if (nums[tmp] == false)
{
nums[tmp] = true;
}
else
{
return false;
}
}
}
}
//lines
for (int i = 0; i < 9; ++i)
{
bool nums[10] = {false};
for (int j = 0; j < 9; ++j)
{
int tmp = board[j][i] - '0';
if (tmp >= 0 && tmp <= 9)
{
if (nums[tmp] == false)
{
nums[tmp] = true;
}
else
{
return false;
}
}
}
}
//fangkuai
for (int i = 0; i < 8; i = i + 3)
{
for (int j = 0; j < 8; j = j + 3)
{
bool nums[10] = {false};
for (int s = i; s < i + 3; s++)
{
for (int t = j; t < j + 3; t++)
{
int tmp = board[s][t]-'0';
if (tmp >= 0 && tmp <= 9)
{
if (nums[tmp] == false)
{
nums[tmp] = true;
}
else
{
return false;
}
}
}
}
}
}
return true;
}
};
int main()
{
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93